package com.fs.sys.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.sys.bean.dto.UserAppDTO;
import com.fs.sys.bean.query.UserAppQuery;
import com.fs.sys.bean.req.UserAppUpdateReq;
import com.fs.sys.entityservice.UserAppEntityService;
import com.fs.common.annotations.Log;
import com.fs.common.bean.LogType;
import com.fs.sys.bean.dto.UserDTO;
import com.fs.sys.bean.query.UserQuery;
import com.fs.sys.entityservice.UserEntityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户应用关系控制器 
 * @author Coder Generator 2025-06-06 11:03:15 
 **/
@RestController
@RequestMapping("/sys/user-app")
@Slf4j
@Tag(name = "用户应用关系控制器")
public class UserAppController {
    @Autowired
    private UserAppEntityService service;

    @Autowired
    private UserEntityService userEntityService;

    @PostMapping("add")
    @Operation(summary = "新增")
    @Log(value = "用户应用", op = LogType.NEW)
    public void add(@RequestBody @Validated UserAppUpdateReq req) {
        String appId = req.getAppId();
        List<UserAppDTO> userAppDTOS = req.getUserIds()
                .stream()
                .map(userId -> {
                    UserAppDTO dto = new UserAppDTO();
                    dto.setUserId(userId);
                    dto.setAppId(appId);
                    return dto;
                }).toList();
        service.insert(userAppDTOS);
    }

    @PostMapping("delete")
    @Operation(summary = "删除")
    @Log(value = "用户应用", op = LogType.DELETE)
    public void delete(@RequestBody UserAppUpdateReq req) {
        service.deleteBuilder()
                .eq("app_id", req.getAppId())
                .in("user_id", req.getUserIds())
                .delete();
    }

    @PostMapping("page-query")
    @Operation(summary = "查询-分页")
    public IPage<UserAppDTO> pageQuery(@RequestBody UserAppQuery query) {
        return service.pageQuery(query);
    }

    @PostMapping("query")
    @Operation(summary = "查询-不分页")
    public List<UserAppDTO> query(@RequestBody UserAppQuery query) {
        return service.query(query);
    }

    @PostMapping("query-by-app")
    public IPage<UserDTO> pageQueryForAppUsers(@RequestBody UserAppQuery query) {
        IPage<UserAppDTO> page = service.pageQuery(query);
        List<String> userIds = page.getRecords().stream()
                .map(UserAppDTO::getUserId)
                .toList();
        if (!CollectionUtils.isEmpty(userIds)) {
            Map<String, UserDTO> userMap = userEntityService.findByIds(userIds)
                    .stream()
                    .collect(Collectors.toMap(UserDTO::getId, u -> u));
            return page.convert(item -> userMap.get(item.getUserId()));
        } else {
            Page<UserDTO> pageData = new Page<>();
            pageData.setTotal(page.getTotal());
            return pageData;
        }
    }

    @PostMapping("query-no-app-user")
    public IPage<UserDTO> pageQueryForNoAppUser(@RequestBody UserAppQuery query) {
        List<String> userIds = service.queryBuilder()
                .eq("app_id", query.getAppId())
                .queryAndConvert(UserAppDTO::getUserId);
        UserQuery userQuery = new UserQuery();
        userQuery.setPageNo(query.getPageNo());
        userQuery.setPageSize(query.getPageSize());
        userQuery.setKey(query.getKey());
        if (!CollectionUtils.isEmpty(userIds)) {
            userQuery.setIdsNotIn(userIds);
        }
        return userEntityService.pageQuery(userQuery);
    }
}